
package botlab.gui.actionlistener;

import botlab.Game;
import botlab.gui.GUI;
import botlab.GameManager;
import botlab.gui.Notifier;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 *
 * @author Quentin
 */
public class StopActionListener implements ActionListener
{
    private GUI gui;
    
    public StopActionListener(GUI gui)
    {
        this.gui = gui;
    }

    public void actionPerformed(ActionEvent e)
    {
        final Game game = GameManager.getInstance().getGame();
        
        //switch to editor panel
        gui.setMode(GUI.EDITOR);
        Notifier.getInstance().enterState("editor");
        
        /* Due to an LWJGL issue: http://lwjgl.org/forum/index.php?topic=4217.0,
         * the JTextPane's text can't be modified after LWGJL canvas grabed keyboard.
         * This very ugly trick consist in opening and closing an invisible window.
         */
        JFrame jf = new JFrame();
        jf.setUndecorated(true);
        jf.setOpacity(0.f);
        jf.setVisible(true);
        jf.setVisible(false);
        jf.dispose();
        jf=null;
        
        // We stop the level
        game.enqueue(new Callable<Void>()
        {
            public Void call()
            {
                    try
                    {
                        game.stopLevel();
                    } 
                    catch (Exception ex)
                    {
                        Logger.getLogger(GUI.class.getName()).log(Level.SEVERE,
                                null, ex);
                    }
                    return null;
            }
        });
        
    }
}
